Imports System.Threading

Module MonitorOrder

    Public MonitorLock As New Object()

    Sub UseResource()
        Randomize()

        Thread.CurrentThread.Sleep(Int(1000 * Rnd()))

        Monitor.Enter(MonitorLock)

        Console.WriteLine("Thread: " & Thread.CurrentThread.Name)

        Monitor.Exit(MonitorLock)

    End Sub

    Sub Main()
        Dim A As Thread = New Thread(AddressOf UseResource)
        Dim B As Thread = New Thread(AddressOf UseResource)
        Dim C As Thread = New Thread(AddressOf UseResource)
        Dim D As Thread = New Thread(AddressOf UseResource)

        A.Name = "A"
        B.Name = "B"
        C.Name = "C"
        D.Name = "D"

        A.Start()
        B.Start()
        C.Start()
        D.Start()

        Console.ReadLine()  
    End Sub

End Module
